package com.runa.rccprotocol.dto.rn;

import com.runa.protocol.dto.ModbusPoint;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.enums.ModbusDataType;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.ModbusRequestData;

/**
 * 2025/5/27 11:01
 * zhangYB
 */
public class RunaBuildValveRequest {


    public static byte[] read188(String serial, int slaveId) {
        byte[] readBytes = read(slaveId);
        return get188Command(serial, readBytes);
    }


    public static byte[] get188Command(String serial, byte[] modbusBytes) {
        byte[] data = new byte[13 + modbusBytes.length];
        int i = 0;
        data[i++] = 0x68;
        data[i++] = (byte)0xff;  // 0x81/0xff

        data[i+6] = 0x11;
        data[i+7] = 0x11;

        if (serial.length() % 2 == 1) {
            serial = "0" + serial;
        }

        byte[] serialBytes = ByteUtil.hexStringToByteFlip(serial);
        for (int i1 = 0; i1 < serialBytes.length; i1++) {
            data[i+i1] = serialBytes[i1];
        }
        i += 7;

        data[i++] = 0x37;
        data[i++] = (byte)(modbusBytes.length & 0xff);

        for (byte b : modbusBytes) {
            data[i++] = b;
        }

        data[i++] = ByteUtil.sum(data, 0, i);

        data[i++] = 0x16;

        return data;

    }

    public static byte[] read(int slaveId) {
        ModbusRequestData modbusRequestData = new ModbusRequestData();
        modbusRequestData.read((byte)(slaveId & 0xff), (byte)0x03, 0, 35);
        return modbusRequestData.getData();
    }

    public static byte[] openControl(int slaveId, int value) {
        ModbusRequestData modbusRequestData = new ModbusRequestData();
        ModbusPoint modbusPoint = new ModbusPoint();
        modbusPoint.setModbusAddress(1);
        modbusPoint.setDataType(ModbusDataType.Modbus_Int16);
        modbusPoint.setByteOrder(ByteOrder.AB);
        modbusPoint.setValue((short)value);
        modbusRequestData.write((byte)(slaveId & 0xff), (byte)0x10, modbusPoint);
        return modbusRequestData.getData();
    }

    public static byte[] openControl188(String serial, int slaveId, int value) {
        byte[] readBytes = openControl(slaveId, value);
        return get188Command(serial, readBytes);
    }

    public static byte[] openLimit188(String serial, Integer slaveId, Integer valveDown, Integer valveUp) {
        byte[] openLimitBytes = openLimit(slaveId, valveDown, valveUp);
        return get188Command(serial, openLimitBytes);
    }

    public static byte[] openLimit(Integer slaveId, Integer valveDown, Integer valveUp) {
        Integer value = (valveDown << 16) + valveUp;
        ModbusRequestData modbusRequestData = new ModbusRequestData();
        ModbusPoint modbusPoint = new ModbusPoint();
        modbusPoint.setModbusAddress(85);
        modbusPoint.setDataType(ModbusDataType.Modbus_Int32);
        modbusPoint.setByteOrder(ByteOrder.ABCD);
        modbusPoint.setValue(value);
        modbusRequestData.write((byte)(slaveId & 0xff), (byte)0x10, modbusPoint);
        return modbusRequestData.getData();
    }


    public static void main(String[] args) {
        byte[] bytes = openControl(0x10, 100);
        System.out.println(ByteUtil.byteToHexString(bytes));
        bytes = openLimit(0x10, 10, 80);
        System.out.println(ByteUtil.byteToHexString(bytes));

        byte[] bytes1 = read188("11223344", 1);
        System.out.println(ByteUtil.byteToHexString(bytes1));

        byte[] bytes2 = openControl188("11223344", 0x10, 100);
        System.out.println(ByteUtil.byteToHexString(bytes2));

    }

}
